import menuConfig from '@/config/menu.config';

import routes from './router.children.config';


export function getOpenKey(pathname){
    let target = routes.find(item => pathname=== item.path);
    return target ? [target.groupid] : ['']
}

//====  需要当前角色能访问的菜单列表

/**
 * [
 *      {
 *          groupName:'试题管理',
 *          groupIcon:"图标",
 *          groupId:'',
 *          chidlren:[
 *              {
 *                  title:'',
 *                  path:''
 *              }
 *          ]
 *      },
 *      ...
 *      {
 *       }
 * ]
 * 
 * */ 

function getMenuView(userViewsData){

    if(!userViewsData.length){
        return []
    }

    //1.根据用户可查看的菜单列表，(userViewsData) ---->  router.children.config(routes)找用户可访问的路由

    let routeViews = routes.filter(item => {
        return userViewsData.find(val => val.view_id === item.view_id)
    })

    let groupIds = [...new Set(routeViews.map(item => item.groupid))];

    return groupIds.map(groupid => {
        let target = menuConfig.find(val => val.id === groupid);
        return {
            groupName:target.groupName,
            groupIcon:target.groupIcon,
            groupId:groupid,
            children:routeViews.filter(item => item.groupid === groupid).map(v => {
                return {path:v.path,title:v.title}
            })
        }
    })
}

export default getMenuView;